package com.spring.cloud.weather.city.util;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.Reader;
import java.io.StringReader;

/**
 * Created by Administrator on 2018/3/17
 */
public class XmlBuilder {

	/**
	 * 将XML转为指定的POJO
	 * @param clazz clazz
	 * @param xmlStr xmlStr
	 * @return Object
	 * @throws Exception Exception
	 */
	public static Object xmlStrToOject(Class<?> clazz, String xmlStr) throws Exception {
		JAXBContext context = JAXBContext.newInstance(clazz);
		
		// XML 转为对象的接口
		Unmarshaller unmarshaller = context.createUnmarshaller();

		Reader reader = new StringReader(xmlStr);
		Object xmlObject = unmarshaller.unmarshal(reader);
		reader.close();

		return xmlObject;
	}
}
